package com.ruoyi.carShop.controller;

import com.ruoyi.carShop.service.IShopOrderDetailsService;
import com.ruoyi.carShop.service.LoadService;
import com.ruoyi.system.api.domain.OrderDetails;
import com.ruoyi.system.api.domain.ServiceDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/carShop")
public class ShopOrderDetailsController {
    private static final Logger log = LoggerFactory.getLogger(ShopOrderDetailsController.class);
    @Autowired
    private IShopOrderDetailsService shopOrderDetailsMapper;
    @Autowired
    private LoadService loadService;
    @RequestMapping("/orderDetails/list")
    public List<OrderDetails> list(OrderDetails orderDetails){
        //1 获取request对象
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        int mId = loadService.getToken(token);
        orderDetails.setMerchantId((long) mId);
        return shopOrderDetailsMapper.selectOrderDetailsList(orderDetails);
    }
    @RequestMapping("/orderDetails/byId")
    public OrderDetails byId(Long orderDetailsId){
        return shopOrderDetailsMapper.selectOrderDetailsByOrderDetailsId(orderDetailsId);
    }

    @RequestMapping("/serviceDetails/name")
    public List<ServiceDetails> selectNameAndPrice(Long orderDetailsId){
        return shopOrderDetailsMapper.selectNameAndPrice(orderDetailsId);
    }

    @RequestMapping("/updateVerification/byId")
    public Integer updateVerificationById(Long orderDetailsId, Date verificationTime){
        return shopOrderDetailsMapper.updateVerificationById(orderDetailsId,verificationTime);
    }
}
